﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PeteBrown.ScreenCapture;

namespace WPFTest.Windows
{
    //TODO: Add functionality that will make the cropping-area to start with the same as previously.
    //TODO: Add functionality to make the cropping-area be moved when the user drags it around.
    /// <summary>
    /// Interaction logic for CaptureWindow.xaml
    /// </summary>
    public partial class CaptureWindow : Window
    {
        #region EVENT_AND_DELEGATES

        public event CaptureScreenHandler OnCaptureScreen;
        public delegate void CaptureScreenHandler(OzFrame frame);

        #endregion

        #region FIELDS
        private double mouseStartX;
        private double mouseStartY;
        private Rectangle r;
        #endregion

        #region CONSTRUCTORS
        public CaptureWindow()
        {
            InitializeComponent();
            InitializeEventHooks();
        }
        #endregion

        #region METHODS

        private void InitializeEventHooks()
        {
            this.KeyUp += new KeyEventHandler(CaptureWindow_KeyUp);
            this.MouseMove += new MouseEventHandler(CaptureWindow_MouseMove);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(CaptureWindow_MouseLeftButtonDown);
        }

        private void DrawMainCroppingRect(MouseEventArgs e, out double left, out double top, out double width, out double height)
        {
            /* Create a rectangle and then add it to our canvas( clear everything else first)
             * Then use some math to determine what our top and left location for the rect is and
             * what it's width and height should be based on the mouse input.
             * Finaly set some colors for the inside of the rect and for the border */

            r = new Rectangle();
            //Line l1 = new Line();
            Cv.Children.Clear();
            Cv.Children.Add(r);
            //Cv.Children.Add(l1);

            if (e.GetPosition(null).X > mouseStartX)
            {
                left = mouseStartX;
                width = e.GetPosition(null).X - mouseStartX;
            }
            else
            {
                left = e.GetPosition(null).X;
                width = mouseStartX - e.GetPosition(null).X;
            }

            if (e.GetPosition(null).Y > mouseStartY)
            {
                top = mouseStartY;
                height = e.GetPosition(null).Y - mouseStartY;
            }
            else
            {
                top = e.GetPosition(null).Y;
                height = mouseStartY - e.GetPosition(null).Y;
            }

            try
            {
                Canvas.SetLeft(r, left);
                Canvas.SetTop(r, top);

                r.Width = width;
                r.Height = height;

                //l1.X1 = Math.Floor(left + (width / 2));
                //l1.Y1 = top;

                //l1.X2 = Math.Floor(left + (width / 2));
                //l1.Y2 = top + height;

            }
            catch (Exception myException)
            {
                MessageBox.Show(myException.ToString());
            }

            // Colors for the rectangle
            Color c = new Color();
            c = Colors.DarkOrange;
            c.A = 125;
            SolidColorBrush brush2 = new SolidColorBrush(c);

            c.A = 255;
            SolidColorBrush brush = new SolidColorBrush(c);
            r.Stroke = brush;
            r.Fill = brush2;
            r.StrokeThickness = 1.0f;
            DoubleCollection dc = new DoubleCollection();
            dc.Add(8);
            dc.Add(4);
            r.StrokeDashArray = dc;

            //l1.Stroke = brush;


        }

        private void CaptureScreen()
        {
            this.Hide();
            BitmapSource bs = ScreenCapture.CaptureRegion((int)Canvas.GetLeft(r), (int)Canvas.GetTop(r), (int)r.Width, (int)r.Height, true);

            this.Show();
            TextInputWindow tInput = new TextInputWindow();
            tInput.ShowDialog();
            string outText = tInput.Text;

            OzImageTextFrame newFrame = new OzImageTextFrame(bs, outText);
            
            if (OnCaptureScreen != null)
            {
                OnCaptureScreen(newFrame);
            }

            this.Hide();
            bs = null;

            this.Close();
        }

        #endregion

        #region EVENTS

        void CaptureWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            mouseStartX = e.GetPosition(null).X;
            mouseStartY = e.GetPosition(null).Y;
        }

        void CaptureWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
            else if (e.Key == Key.Enter)
            {
                CaptureScreen();
            }
        }

        void CaptureWindow_MouseMove(object sender, MouseEventArgs e)
        {
            //TODO: Add cool triangles on the corners to make it look better.
            //TODO: Add grid-like features to make it easier to look at the current size.
            if (e.LeftButton == MouseButtonState.Pressed)
            {

                double left;
                double top;
                double width;
                double height;
                DrawMainCroppingRect(e, out left, out top, out width, out height);
                
            }
        }

        #endregion

    }
}
